package com.xudow.app.newui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.activeshare.edu.ucenter.models.user.UserProfileWithOther;
import com.xudow.app.Config;
import com.xudow.app.R;
import com.xudow.app.util.ImageUtils;

import java.util.List;

/**
 * 作者：LownChen on 2016/5/27 12:28
 */
public class ChildsGridViewAdapter extends BaseAdapter {
    private Context context;
    private List<UserProfileWithOther> data;

    public ChildsGridViewAdapter(Context context, List<UserProfileWithOther> data) {
        this.context = context;
        this.data = data;
    }
    public void setData( List<UserProfileWithOther> data){
        this.data = data;
        notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        if (data==null||data.size()==0)
            return 1;
        return data.size()+1;
    }

    @Override
    public Object getItem(int position) {
        if (position<getCount())
            return data.get(position);
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View v, ViewGroup parent) {
        ViewHolder holder;
        if (null==v){
            if (position==data.size()) {
                v = View.inflate(context, R.layout.item_myfragment_child_gv_add, null);
                return v;
            }else {
                holder=new ViewHolder();
                v = View.inflate(context, R.layout.item_myfragment_child_gv, null);
                holder.name=(TextView)v.findViewById(R.id.child_name_tv);
                holder.ico=(ImageView)v.findViewById(R.id.child_ico_iv);
                v.setTag(holder);
            }

        }else
           holder=(ViewHolder)v.getTag();
           UserProfileWithOther child=data.get(position);
            String imageUrl = String.format(Config.IMAGE_LOAD_URL_PARAMS, child.getPhotoPath());
            holder.name.setText(child.getName());
            ImageUtils.loadImage(context,holder.ico,imageUrl,R.mipmap.test2);
        return v;
    }
    class ViewHolder{
        TextView name;
        ImageView ico;
    }
}
